package test.annotaion;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AnnoParse {
	private Map classFileList = new HashMap();
	private int pathArrayCount;
	
	static void testDoA() {
		try {
            for (Method method : AnnoParse.class
                    .getClassLoader()
                    .loadClass((""))
                    .getMethods()) {
                // checks if MethodInfo annotation is present for the method
                if (method
                        .isAnnotationPresent(OnjInfo.class)) {
                    try {
                        // iterates all the annotations available in the method
                        for (Annotation anno : method.getDeclaredAnnotations()) {
                            System.out.println("Annotation in Method '"
                                    + method + "' : " + anno);
                        }
                        OnjInfo methodAnno = method
                                .getAnnotation(OnjInfo.class);
//                        if (methodAnno.year() == "2200") {
                        if ("2200".equals(methodAnno.year())) {
                            System.out.println("forever!! oraclejava, onj"+ method);
                        }
 
                    } catch (Throwable ex) {
                        ex.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	void testDoB(String path) {
		try {
			File file = new File(path);
			File[] fList = file.listFiles();
			
			for(File f : fList) {
				if (f.isFile()) {
//					System.out.println("File : " + f.getName());
					String javaPath = f.getPath().substring(f.getPath().lastIndexOf("test\\annotaion\\")).replace("\\", ".").replace(".java", "");
					classFileList.put(pathArrayCount++, javaPath);
//					System.out.println("java Path : " + javaPath);
				} else if (f.isDirectory()) {
					testDoB(f.getCanonicalPath().toString());
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	 
	void testDoC(String path) {
		testDoB(path);
		
		try {
			for (int i=0; i<classFileList.size(); i++) {
//			System.out.println(i + " "+ classFileList.get(i));
				for (Method method : AnnoParse.class.getClassLoader()
						                            .loadClass((classFileList.get(i).toString()))
						                            .getMethods()) {
					if (method.isAnnotationPresent(OnjInfo.class)) {
						try {
							
							OnjInfo methodAnno = method.getAnnotation(OnjInfo.class);
							if ("2014".equals(methodAnno.year())) {
//								method.invoke("name", "c");
								Class clz = Class.forName(classFileList.get(i).toString());
								clz.newInstance().getClass().getMethod(method.getName()).invoke(method.getName());
								
							}
							
						} catch (Throwable te) {
							te.printStackTrace();
						}
					}
				}
				
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static void main(String[] args) {
		String path = "./src/test";
		AnnoParse test = new AnnoParse();
		test.testDoC(path);
    }
}
